//package com.asset.crm.dao;
//
//import java.text.DateFormat;
//import java.text.SimpleDateFormat;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//
//import javax.servlet.http.HttpSession;
//
//import org.hibernate.Query;
//import org.hibernate.Session;
//
//import com.asset.crm.domain.Appointment;
//import com.asset.crm.domain.Calendar;
//import com.asset.crm.domain.Call;
//import com.asset.crm.domain.Employee;
//import com.asset.crm.domain.Lead;
////import com.asset.crm.service.AppointmentService;
////import com.asset.crm.service.CalendarService;
////import com.asset.crm.service.CallService;
//
//public class CalendarDaoImpl extends BasicDaoImpl<Calendar> {
//
////	@Autowired
////	private CalendarService calendarService;
////	
////	@Autowired
////	private AppointmentService appointmentService;
////	
////	@Autowired
////	private CallService callService;
//	public Calendar getEventByCallId(Integer parentId) {
//		Session session = sessionFactory.getCurrentSession();
//		String eventType = "call";
//		Query query = session.createQuery("from Calendar where  parentId = :parentId and eventType = :eventType");
//		query.setParameter("parentId", parentId);
//		query.setParameter("eventType", eventType);
//		@SuppressWarnings("unchecked")
//		List<Calendar>  events= query.list();
//		System.out.println("><><><><><><+++++++++++++++++++++++++++++"+events.size());
//		if(events.size() > 0)
//		return events.get(0);
//		else
//		return null;
//	}
//	public List<Calendar>getEvents(Integer empId){
//		Session session = sessionFactory.getCurrentSession();
//		Query query = session.createQuery("from Calendar where  empId.id = :empId");
//		query.setParameter("empId", empId);
//		List<Calendar>  events= query.list();
//		System.out.println("><><><><><><+++++++++++++++++++++++++++++"+events.size());
//		if(events.size() > 0)
//		return events;
//		else
//		return new ArrayList<Calendar>();
//	}
//	public void callEvent(Call call) {
//		Calendar calEvent;
//		if(call.getId() != null){
//			calEvent = getEventByCallId(call.getId());
//			if(calEvent == null){
//				calEvent = new Calendar();
//			}
//		}
//		else {
//			calEvent = new Calendar();
//		}
//		if(call.getId() != null){
//			getEventByCallId(call.getId());
//			calEvent.setParentId(call.getId());
//			calEvent.setEventType("call");
//		}
//		if(call.getDate() != null){ calEvent.setStartDate(call.getDate()); }
//		if( call.getComment() != null){ calEvent.setDescription(call.getComment()); }
//		if( (call.getCrmStaff()).getfName() != null){ calEvent.setTitle(call.getCrmStaff().getfName()); }
//		if( call.getContactedPerson() != null){ calEvent.setNote(call.getContactedPerson().getfName()); }
//		if( call.getDate() != null){ calEvent.setEndDate(call.getDate()); }
//		if( call.getCrmStaff() != null){ calEvent.setEmpId( call.getCrmStaff()); System.out.println("empid is "+call.getCrmStaff()); }
//		//added by hisham
//		if(call.getStartTime()!=null){
//			String[] startDate = call.getStartTime().split(":");
//			int hour =Integer.parseInt(startDate[0]);
//			int minite = Integer.parseInt(startDate[1]);
//			Date date = call.getDate();                      // timestamp now
//			java.util.Calendar cal = java.util.Calendar.getInstance();       // get calendar instance
//			cal.setTime(date);                           // set cal to date
//			cal.set(java.util.Calendar.HOUR_OF_DAY, hour);            // set hour to midnight
//			cal.set(java.util.Calendar.MINUTE, minite);             // set millis in second
//			Date endDate = cal.getTime(); 
//			System.out.println(">>>>>>>>>>>>>><<<<<<<<<<<<<<<<>>>>>>>>>>>>>>><<<<<<<<<<<"+endDate.toString());
//			calEvent.setStartDate(endDate);
//		}
//		if(call.getEndTime()!=null){
//			String[] endDate = call.getEndTime().split(":");
//			int hour =Integer.parseInt(endDate[0]);
//			int minite = Integer.parseInt(endDate[1]);
//			Date date = call.getDate();                      // timestamp now
//			java.util.Calendar cal = java.util.Calendar.getInstance();       // get calendar instance
//			cal.setTime(date);                           // set cal to date
//			cal.set(java.util.Calendar.HOUR_OF_DAY, hour);            // set hour to midnight
//			cal.set(java.util.Calendar.MINUTE, minite);             // set millis in second
//			Date finalDate = cal.getTime(); 
//			System.out.println(">>>>>>>>>>>>>><<<<<<<<<<<<<<<<>>>>>>>>>>>>>>><<<<<<<<<<<"+endDate.toString());
//			calEvent.setEndDate(finalDate);
//		}
//		sessionFactory.getCurrentSession().saveOrUpdate(calEvent);
//	}
//	
//	public Calendar getEventByAppointmentId(Integer parentId) {
//		Session session = sessionFactory.getCurrentSession();
//		String eventType = "appointment";
//		Query query = session.createQuery("from Calendar where  parentId = :parentId and eventType = :eventType");
//		query.setParameter("parentId", parentId);
//		query.setParameter("eventType", eventType);
//		@SuppressWarnings("unchecked")
//		List<Calendar>  events= query.list();
//		System.out.println("><><><><><><+++++++++++++++++++++++++++++"+events.size());
//		if(events.size() > 0)
//		return events.get(0);
//		else
//		return null;
//	}
//	
//	public void appointmentEvent(Appointment appointment) {
//		Calendar calEvent;
//		if(appointment.getId() != null){
//			calEvent = getEventByAppointmentId(appointment.getId());
//			if(calEvent == null){
//				calEvent = new Calendar();
//			}
//		}
//		else {
//			calEvent = new Calendar();
//		}
//		if( appointment.getId() != null){
//			calEvent.setParentId(appointment.getId());
//			calEvent.setEventType("appointment");
//		}
//		if( appointment.getDate() != null){ 
//			String[] startDate = appointment.getTime().split(":");
//			int hour =Integer.parseInt(startDate[0]);
//			int minite = Integer.parseInt(startDate[1]);
//			Date date = appointment.getDate();                      // timestamp now
//			java.util.Calendar cal = java.util.Calendar.getInstance();       // get calendar instance
//			cal.setTime(date);                           // set cal to date
//			cal.set(java.util.Calendar.HOUR_OF_DAY, hour);            // set hour to midnight
//			cal.set(java.util.Calendar.MINUTE, minite);             // set millis in second
//			Date endDate = cal.getTime(); 
//			System.out.println(">>>>>>>>>>>>>><<<<<<<<<<<<<<<<>>>>>>>>>>>>>>><<<<<<<<<<<"+endDate.toString());
//			calEvent.setStartDate(endDate);
//			}
//		if( appointment.getComment() != null){ calEvent.setDescription(appointment.getComment()); }
//		if( (appointment.getSalesMan()).getfName() != null){ calEvent.setTitle(appointment.getSalesMan().getfName()); }
//		if( appointment.getContactedPerson() != null){ calEvent.setNote(appointment.getContactedPerson().getfName()); }
//		if( appointment.getMeetingDuration() != null){
//			String[] startDate = appointment.getMeetingDuration().split(":");
//			int hour =Integer.parseInt(startDate[0]);
//			int minite = Integer.parseInt(startDate[1]);
//			Date date = appointment.getDate();                      // timestamp now
//			java.util.Calendar cal = java.util.Calendar.getInstance();       // get calendar instance
//			cal.setTime(date);                           // set cal to date
//			int endHour =cal.get(java.util.Calendar.HOUR_OF_DAY);
//			System.out.println("calendar hour is:"+endHour);
//			endHour = endHour+hour;// set hour to midnight
//			int endMinite =cal.get(java.util.Calendar.MINUTE);
//			System.out.println("calendar minute is:"+endMinite);
//			endMinite =endMinite+minite; // set minute in hour
//			cal.set(java.util.Calendar.HOUR_OF_DAY, endHour);            // set hour to midnight
//			cal.set(java.util.Calendar.MINUTE, endMinite); 
//			cal.get(java.util.Calendar.HOUR_OF_DAY);
//			Date endDate = cal.getTime(); 
//			System.out.println(">>>>>>>>>>>>>><<<<<<<<<<<<<<<<>>>>>>>>>>>>>>><<<<<<<<<<<"+endDate.toString());
//			calEvent.setEndDate(endDate); 
//			}
////		if( appointment.getFollowUpDate() != null){
////			String[] startDate = appointment.getTime().split(":");
////			int hour =Integer.parseInt(startDate[0]);
////			int minite = Integer.parseInt(startDate[1]);
////			Date date = appointment.getFollowUpDate();
////			java.util.Calendar cal = java.util.Calendar.getInstance();
////			cal.setTime(date);                           // set cal to date
////			cal.set(java.util.Calendar.HOUR_OF_DAY, hour);            // set hour to midnight
////			cal.set(java.util.Calendar.MINUTE, minite);             // set millis in second
////			Date endDate = cal.getTime(); 
////			System.out.println(">>>>>>>>>>>>>><<<<<<<<<<<<<<<<>>>>>>>>>>>>>>><<<<<<<<<<<"+endDate.toString());
////			calEvent.setStartDate(endDate);
////		}
//		if( appointment.getSalesMan() != null)
//		{ 
//			calEvent.setEmpId(appointment.getSalesMan()); 
//		}
//		sessionFactory.getCurrentSession().saveOrUpdate(calEvent);
//	}
//	
//	public List<Calendar> getAllEventsByEmplyoeeID(Integer id){
//		String hqlQuery = "select cale from Calendar as cale join cale.parentId as parent , Call as call join call.crmStaff as callemp , Appointment as app join app.salesMan as appemp where ( parent = callemp.id and cale.evenType = 'call' ) or ( parent = appemp.id and cale.eventType = 'appointment' ) " ;
//				
//		Session session = sessionFactory.getCurrentSession();
//		Query query = session.createQuery(hqlQuery);
////		query.setInteger("employeeId", id);
//		System.out.println("Calendar list number = "+query.list().size());
//		return query.list();
//	}
//	public List<Calendar> getEventsbyEmployeeId(Integer empId) {
//		Session session = sessionFactory.getCurrentSession();
//		String eventType = "appointment";
//		String eventType1 = "call";
//		Query query = session.createQuery("from Calendar where  empId.id = :empId and eventType = :eventType or empId.id= :empId and eventType = :eventType1 ");
//		query.setParameter("empId", empId);
//		query.setParameter("eventType",eventType);
//		query.setParameter("eventType1",eventType1);
//		@SuppressWarnings("unchecked")
//		List<Calendar>  events= query.list();
//		System.out.println("><><><><><><+++++++++++++++++++++++++++++"+events.size());
//		if(events.size() > 0)
//			return events;
//		else
//			return new ArrayList<Calendar>();
//	}
//	public List<Calendar> getAppointmentEventsByEmployeeId(Integer empId) {
//		Session session = sessionFactory.getCurrentSession();
//		String eventType = "appointment";
//		Query query = session.createQuery("from Calendar where  empId.id = :empId and eventType = :eventType");
//		query.setParameter("empId", empId);
//		query.setParameter("eventType",eventType);
//		@SuppressWarnings("unchecked")
//		List<Calendar>  events= query.list();
//		System.out.println("><><><><><><+++++++++++++++++++++++++++++"+events.size());
//		if(events!=null && events.size() > 0)
//		return events;
//		else
//		return new ArrayList<Calendar>();
//	}
//	public List<Calendar> getCallstByEmployeeID(Integer empId){
//		Session session = sessionFactory.getCurrentSession();
//		String eventType = "call";
//		Query query = session.createQuery("from Calendar where  empId.id = :empId and eventType = :eventType");
//		query.setParameter("empId", empId);
//		query.setParameter("eventType",eventType);
//		@SuppressWarnings("unchecked")
//		List<Calendar>  events= query.list();
//		System.out.println("><><><><><><+++++++++++++++++++++++++++++"+events.size());
//		if(events!=null && events.size() > 0)
//		return events;
//		else
//		return new ArrayList<Calendar>();
//	}
//	public void getCallEventssNo(HttpSession session, Model model){
//			Integer callEvents = 0;
//		  if(null !=session.getAttribute("userDetails") ){
//		   Employee employee = (Employee)session.getAttribute("userDetails");
//		   Session session2 = sessionFactory.getCurrentSession();
//		   List<Calendar>callsEvents = getCallstByEmployeeID(employee.getId());
//		   List<Calendar>finalEvents = new ArrayList<Calendar>();
//		   for (Calendar calendar : callsEvents) {
//			  try {
//				  DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//				   Date date = new Date();
//				   String startdate = calendar.getStartDate().toString().substring(0, 10);
//				   String newDate = dateFormat.format(date).toString();
//				   System.out.println(dateFormat.format(date).toString());
//				   Date date1 = dateFormat.parse(startdate);
//				   Date date2 = dateFormat.parse(newDate);
//				   if (date1.compareTo(date2) >= 0) {
//				        System.out.println("success");
//				        finalEvents.add(calendar);
//				    }
//			} catch (Exception e) {
//				// TODO: handle exception
//			}
//		}
//		   System.out.println("final events:-"+finalEvents.size());
//		   callEvents = finalEvents.size();
//		   System.out.println("events is "+callEvents);
//		}
//		   
//		  
//		  model.addAttribute("callEvents",callEvents);
//	}
//	
//	public void getAppointmentEventsNo(HttpSession session, Model model){
//		  Integer appointmentEvents = 0;
//		  if(null !=session.getAttribute("userDetails") ){
//			   Employee employee = (Employee)session.getAttribute("userDetails");
//			   Session session2 = sessionFactory.getCurrentSession();
//			   List<Calendar>appointEvents = getAppointmentEventsByEmployeeId(employee.getId());
//			   List<Calendar>finalEvents = new ArrayList<Calendar>();
//			   for (Calendar calendar : appointEvents) {
//				  try {
//					  DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//					   Date date = new Date();
//					   String startdate = calendar.getStartDate().toString().substring(0, 10);
//					   String newDate = dateFormat.format(date).toString();
//					   System.out.println(dateFormat.format(date).toString());
//					   Date date1 = dateFormat.parse(startdate);
//					   Date date2 = dateFormat.parse(newDate);
//					   if (date1.compareTo(date2) >= 0) {
//					        System.out.println("success");
//					        finalEvents.add(calendar);
//					    }
//				} catch (Exception e) {
//					// TODO: handle exception
//				}
//			}
//			   System.out.println("final events:-"+finalEvents.size());
//			   appointmentEvents = finalEvents.size();
//			   System.out.println("events is "+appointmentEvents);
//			}
//		  model.addAttribute("appointmentEvents",appointmentEvents);
//		 }
//}
